
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>

#include "words.h"
#include "loader.h"

#define _LOADER_PRINT_LOG 0

using namespace std;

int WordFileLoader::LoadFrom(const char *path, vector<Word> &out) {
    ifstream file(path);

    if (!file.good()) {
        return LD_ERR_NO_FILE;
    }

    FreeAllWords(out);
    out.clear();

    string word;
    string desc;
    char _;

    while (file >> word) {
        file >> _;  // eat ':'
        getline(file, desc, '\n');

#if _LOADER_PRINT_LOG

        cout << "[loader] Loaded \""<< word
             << "\"(" << desc << ")" << endl;

#endif // _LOADER_PRINT_LOG

        char *tp = new char[50];
        char *dp = new char[100];

        strcpy(tp, word.c_str());
        strcpy(dp, desc.c_str());

        out.push_back(Word{tp, dp});
    }
    return 0;
}
